from flask import Blueprint, request, jsonify
from api.services.data_service import DataService
from api.services.type_service import TypeService

bp = Blueprint('data', __name__)


VIDEO_TYPES = [
    {"id": 1, "name": "电影1"},
    {"id": 2, "name": "电视剧"}
]

# 新增：获取视频类型列表的接口
@bp.route('/video_types', methods=['GET'])
def get_video_types():
    data = TypeService.get_by_show()
    return jsonify({
        "status": "success",
        "data": data
    })

# 新增：获取视频地址的接口
@bp.route('/data', methods=['POST'])
def get_data():
   # 添加JSON格式检查
    if not request.is_json:
        return jsonify({"error": "请求必须是JSON"}), 400
    
    try:
        data = request.get_json()  # 更安全的获取JSON方式
    except:
        return jsonify({"error": "JSON格式无效"}), 400
    
    video_url = data.get('video_url')
    video_type_id = data.get('video_type')
    
    if not video_url:
        return jsonify({"error": "需要视频URL"}), 400
    
    if not video_type_id:
        return jsonify({"error": "视频类型为空"}), 400

    
    # 返回模拟数据
    return jsonify({
        "status": "success",
        "title": "示例视频",
        "duration": 360,
        "resolution": "1920x1080",
        "video_type_id": video_type_id,
        "analysis": {
            "人物数量": 3,
            "主要对象": "api接口",
            "场景类型": "户外",
            "情感分析": video_url
        }
    })


@bp.route('/test', methods=['POST'])
def login():
    if not request.is_json:
        return jsonify({"error": "请求必须是JSON"}), 400
    
    try:
        data = request.get_json()  # 更安全的获取JSON方式
    except:
        return jsonify({"error": "JSON格式无效"}), 400
    
    video_type = data.get('video_type')
    video_url = data.get('video_url')
    
    if not video_type:
        return jsonify({"error": "需要video_type"}), 400
    if not video_url:
        return jsonify({"error": "需要video_url"}), 400
    if video_type:
        return jsonify({
            "message": "success",
            "data": result
        })
    # 2. 调用服务层处理数据
    # 简单处理可以直接调用单个方法
    # user_data = DataService.login(username,password)
    # if user_data:
    #     return jsonify({
    #         "message": "success",
    #         "data": user_data
    #     })
    # else:
    #     return jsonify({
    #         "message": "error",
    #         "data": "用户名或密码错误"
    #     })